package com.david.utils;

import org.springframework.beans.factory.annotation.Value;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
import org.springframework.context.annotation.PropertySource;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ConvertUtils {


    public static String officeConvertPdf(String location,String officeFilePath,String pdfFilePath) {

        // office文件转换为pdf文件
        jacobUtil.word2PDF(officeFilePath, pdfFilePath);

        List<String> imgURIs = ConvertUtils.pdfConvertImg(location,pdfFilePath);
        String content = "";
        for(String imgurl : imgURIs){
            content += "<img src='" + imgurl + "' />";
        }
        return content;
    }
    /**
     *
     * @Title: pdfConvertImg
     * @Description: PDF文件转图片，返回图片数据集
     * @author huxin
     * @date 2017年8月17日 下午7:37:02
     * @param
     * @return
     */
    public static List<String> pdfConvertImg(String location,String pdfFilePath){
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
        PDDocument doc = null;
        long beginTime = System.nanoTime();
        List<String> imgURIs = new ArrayList<String>();
        try {
            String imgURI = null;// 图片地址
            doc = PDDocument.load(new File(pdfFilePath));
            int pageSize = doc.getNumberOfPages();
            System.out.println(pageSize);
            PDFRenderer pdfRenderer = new PDFRenderer(doc);
            byte[] fileByte = null;
            for (int page = 0; page < pageSize; ++page) {
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 150F);
                ImageIOUtil.writeImage(bim, "JPG", os);
                try {
                    fileByte = os.toByteArray();
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSS");
                    String formDate = sdf.format(date);
                    String fileName = formDate + ".JPG";
                    String filePath = location + "/images/";
                    buff2Image(fileByte,filePath + File.separator + fileName);
                    imgURI = "/uploads/" + fileName;
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (os != null) {
                        os.close();
                    }
                }
                imgURIs.add(imgURI);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (doc != null)
                try {
                    doc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        long endTime = System.nanoTime();
        long l = endTime - beginTime;
        System.out.println("----------------------------差时：" + l + "耗时: " + l / 1000000000 + " 秒");
        return imgURIs;
    }
    static void buff2Image(byte[] b,String tagSrc) throws Exception
    {
        FileOutputStream fout = new FileOutputStream(tagSrc);
        //将字节写入文件
        fout.write(b);
        fout.close();
    }

}
